//
// Created by 匡涂上青 on 2021/8/9.
//

#ifndef KGL_SCRIPTABLE_H
#define KGL_SCRIPTABLE_H

#include "kgl.h"
#include <vector>

class ScriptManager;

class Scriptable {
public:
    virtual void OnSetup() = 0;

    virtual void OnUpdate() = 0;

    virtual void OnDestroy() = 0;

protected:
    friend ScriptManager;

    ~Scriptable() = default;
};

class ScriptManager {
public:
    template<typename T>
    void add_script(T *script) {
        if (!script) return;
        auto deleter = [](T *ptr) {
            delete ptr;
        };
        scripts.push_back(kgl::Ref<Scriptable>(script, deleter));
    }

    void Setup() {
        for (auto &script : scripts)script->OnSetup();
    }

    void Update() {
        for (auto &script : scripts)script->OnUpdate();
    }

    void Destroy() {
        for (auto &script : scripts)script->OnDestroy();
    }

private:
    std::vector<kgl::Ref<Scriptable>> scripts;
};

#endif //KGL_SCRIPTABLE_H
